import pandas as pd
from docx import Document
from docx.shared import Pt

# 定义各模型参数
word2vec_params = {
    "vector_size": 100,
    "window": 5,
    "min_count": 1,
    "workers": 4
}

textcnn_params = {
    "input_dim": 10,
    "output_dim": 100,
    "filter_sizes": "[2, 3, 4]",
    "num_filters": 16
}

transformer_params = {
    "input_dim": 100,
    "output_dim": 1,
    "nhead": 5,
    "num_layers": 2
}

training_and_data_params = {
    "optimizer": "Adam",
    "learning_rate": 0.001,
    "criterion": "nn.MSELoss()",
    "epochs": 100,
    "max_history_length": 10
}

# 定义将参数字典转换为 DataFrame 的函数
def params_to_df(params, model_name):
    data = []
    for param_name, param_value in params.items():
        data.append([model_name, param_name, param_value])
    return pd.DataFrame(data, columns=["模型名称", "参数名称", "参数值"])

# 将各参数字典转换为 DataFrame
word2vec_df = params_to_df(word2vec_params, "Word2Vec")
textcnn_df = params_to_df(textcnn_params, "TextCNN")
transformer_df = params_to_df(transformer_params, "TransformerModel")
training_and_data_df = params_to_df(training_and_data_params, "训练与数据处理")

# 创建 Word 文档
doc = Document()

# 添加主标题
doc.add_heading('深度学习模型参数表格', 0)

# 定义将 DataFrame 添加到 Word 表格的函数
def add_df_to_doc(doc, df, table_title):
    doc.add_heading(table_title, 1)
    table = doc.add_table(rows=1, cols=len(df.columns))
    hdr_cells = table.rows[0].cells

    # 设置表头
    for i, col in enumerate(df.columns):
        hdr_cells[i].text = col
        for paragraph in hdr_cells[i].paragraphs:
            for run in paragraph.runs:
                run.font.size = Pt(12)

    # 添加数据到表格
    for index, row in df.iterrows():
        row_cells = table.add_row().cells
        for i, value in enumerate(row):
            row_cells[i].text = str(value)
            for paragraph in row_cells[i].paragraphs:
                for run in paragraph.runs:
                    run.font.size = Pt(10)

# 依次添加各表格到文档
add_df_to_doc(doc, word2vec_df, "Word2Vec 模型参数")
add_df_to_doc(doc, textcnn_df, "TextCNN 模型参数")
add_df_to_doc(doc, transformer_df, "TransformerModel 模型参数")
add_df_to_doc(doc, training_and_data_df, "训练与数据处理参数")

# 保存 Word 文档
doc.save('model_parameters_four_tables.docx')